/*
 * Dijjer - A Peer to Peer HTTP Cache
 * Copyright (C) 2004,2005 Change.Tv, Inc
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

package dijjer.util.logging;

import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;

public class DefaultLogHandler implements LogHandler {

	public static final String VERSION = "$Id: DefaultLogHandler.java 2 2008-03-21 14:33:03Z ian.clarke $";

	private static final SimpleDateFormat _date = new SimpleDateFormat("[MM-dd-yyyy HH:mm:ss]");

	private final PrintWriter _out;
	private final boolean _printDate;
	private final boolean _printTrace;
	private final boolean _printLevel;
	private boolean _active = true;

	public DefaultLogHandler(PrintWriter writer, boolean printLevel, boolean printDate, boolean printTrace) {
		_out = writer;
		_printLevel = printLevel;
		_printDate = printDate;
		_printTrace = printTrace;
	}

	public final void setActive(boolean active) {
		_active = active;
	}

	public final boolean isActive() {
		return _active;
	}

	public void log(Level level, Date time, String message, String sourceClass,
	    String sourceMethod, int sourceLine, Throwable thrown) {
		if (_printLevel) {
			_out.print('[');
			_out.print(level);
			_out.print(']');
			_out.print(' ');
		}
		if (_printDate) {
			_out.print(_date.format(time));
			_out.print(' ');
		}
		if (_printTrace) {
			_out.print(sourceClass);
			_out.print('.');
			_out.print(sourceMethod);
			_out.print("(line:");
			_out.print(sourceLine);
			_out.print(") ");
		}
		if (message != null) {
			_out.print(message);
		}
		_out.println();
		if (thrown != null) {
			thrown.printStackTrace(_out);
		}
		_out.flush();
	}
}
